package ua.kpi_java_training.evgeniygusar.module2;

import java.util.ArrayList;

public class AllVictims {

    int VICTIM_BIRTH_CYCLE = 3;

    int index_of_nearest_victim = 0;
    int X_BORDER, Y_BORDER;
    Pool mypool = new Pool();

    public AllVictims() {

        X_BORDER = mypool.getXBorder();
        Y_BORDER = mypool.getYBorder();
    }

    int x_of_nearest_victim = X_BORDER+1, y_of_nearest_victim = Y_BORDER+1;

    static private ArrayList<Victim> victims = new ArrayList<Victim>();

    public int getVictimBirthCycle() {
        return VICTIM_BIRTH_CYCLE;
    }

    public void addVictim() {
        victims.add(new Victim());
    }

    public Victim getVictim(int position) {
        return victims.get(position);
    }

    public void removeVictim(int position) {
        victims.remove(position);
    }

    public void victimsMoving() {
        for(Victim v: victims) {
            v.Moving();
        }
    }

    public void displayVictimsMoving() {
        for(Victim v: victims) {
            System.out.print("Victim " + (victims.indexOf(v) + 1) + ": x = " + v.x);
            System.out.println(", y = " + v.y);
        }
    }

    public int numbersOfVictims() {
        return victims.size();
    }

    public void nearestVictim(int x, int y) {
        double shortest_distance, current_distance;

        shortest_distance = (X_BORDER+1)*(X_BORDER+1) + (Y_BORDER+1)*(Y_BORDER+1);
        for(Victim v: victims) {
            current_distance = Distance(x-v.x, y-v.y);
            if(current_distance < shortest_distance) {
                shortest_distance = current_distance;
                index_of_nearest_victim = victims.indexOf(v);
                x_of_nearest_victim = v.x;
                y_of_nearest_victim = v.y;
            }
        }
    }

    public double Distance(int x, int y) {
        return Math.sqrt(x*x + y*y);
    }

    public int getXOfNearestVictim(){
        return x_of_nearest_victim;
    }

    public int getYOfNearestVictim(){
        return y_of_nearest_victim;
    }

    public int getIndexOfNearestVictim(){
        return index_of_nearest_victim;
    }
}
